import legacy from '@vitejs/plugin-legacy';
import react from '@vitejs/plugin-react';
import path from 'path';
import { defineConfig, loadEnv } from 'vite';
import { createStyleImportPlugin, NutuiResolve } from 'vite-plugin-style-import';
import tsconfigPaths from 'vite-tsconfig-paths';
// https://vitejs.dev/config https://vitest.dev/config
export default defineConfig(({ mode }) => {
  // 根据当前工作目录中的 `mode` 加载 .env 文件
  // 设置第三个参数为 '' 来加载所有环境变量，而不管是否有 `VITE_` 前缀。
  const env = loadEnv(mode, path.join(__dirname, 'env'), '');

  return {
    // vite 配置
    publicDir: './public',
    define: {
      __APP_ENV__: JSON.stringify(env.VITE_APP_ENV),
      __BASE_URL__: JSON.stringify(env.VITE_BASE_URL)
    },
    envDir: 'env',
    plugins: [
      tsconfigPaths(),
      react(),
      legacy({
        targets: ['Chrome 69'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        modernPolyfills: true
      }),
      createStyleImportPlugin({
        resolves: [NutuiResolve()],
        libs: [
          // If you don’t have the resolve you need, you can write it directly in the lib, or you can provide us with PR
          {
            libraryName: '@nutui/nutui-react',
            libraryNameChangeCase: 'pascalCase',
            resolveStyle: (name) => {
              return `@nutui/nutui-react/dist/esm/${name}/style`;
            }
          }
        ]
      })
    ],
    css: {
      preprocessorOptions: {
        scss: {
          // 配置 nutui 全局 scss 变量
          additionalData: `@import "@nutui/nutui-react/dist/styles/variables.scss";;@import "./src/styles/custom_theme.scss";`
        }
      }
    },
    resolve: {
      alias: {
        '@': path.join(__dirname, 'src'),
        '^': path.join(__dirname, 'assets')
      }
    },
    server: {
      host: true,
      proxy: {
        '/apiProxy': {
          target: 'https://szbank.okaok.com.cn',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/apiProxy/, '')
        }
      }
    }
  };
});
